//
//  MineInviteViewController.m
//  QA
//
//  Created by 李小龙 on 2017/5/22.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "MineInviteViewController.h"
#import "MineInviteModel.h"
#import "MineInviteTableViewCell.h"

@interface MineInviteViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *QRCodeImageView;
@property (weak, nonatomic) IBOutlet CustomEditLabel *inviteCodeLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;// %@的邀约码

@property (nonatomic, strong) NSMutableArray *tableArray;
//@property (nonatomic, assign) NSInteger page;

@end

@implementation MineInviteViewController

static NSString *mineInviteIdentifier = @"mineInviteIdentifier";
#pragma mark - get
- (NSMutableArray *)tableArray {
    if (!_tableArray) {
        _tableArray = [[NSMutableArray alloc] init];
    }
    return _tableArray;
}
#pragma mark - viewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"推荐好友";
    [self hiddenShadowImage:NO];
    
    [self.nameLabel setText:[NSString stringWithFormat:@"%@的邀约码",[UserInfo shareInstance].user.mnickname]];
    
    [self.QRCodeImageView setCircleWithColor:[UIColor colorWithHex:0xEAEAEA alpha:1.0] borderWidth:0.5 cornerRadius:3.0];
    
//    self.page = 1;
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        _page = 1;
//        [self getNetData];
//    }];
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        _page++;
//        [self getNetData];
//    }];
    
    [self.tableView clearExtendCellLine];
    [self.tableView registerNib:[UINib nibWithNibName:@"MineInviteTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:mineInviteIdentifier];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [self getInviteCode];
    [self getNetData];
}

//// 获取我的邀约码
//- (void)getInviteCode {
//    __weak typeof(self) weakSelf = self;
//    [MYAFNetworking POST:URL_Get_Minvitation parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
//        if ([responseObject[@"code"] isEqualToString:@"200"]) {
//            [weakSelf.inviteCodeLabel setText:responseObject[@"data"][@"minvitationcode"]];
//            [weakSelf.QRCodeImageView sd_setImageWithURL:GetUrl(responseObject[@"data"][@"qrcode"]) placeholderImage:PlaceholderImagePictures];
//        } else {
//            [MBProgressHUD showError:weakSelf.view title:@"" code:responseObject[@"code"] animated:YES];
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
//        [MBProgressHUD showError:weakSelf.view title:@"" code:@"" animated:YES];
//    }];
//}
// 获取列表数据
- (void)getNetData {
    
    __weak typeof(self) weakSelf = self;
    //@{@"pageNumber":[NSString stringWithFormat:@"%ld",(long)self.page],@"pageSize":PageSize}
    [MYAFNetworking POST:URL_Get_Minvitation parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
//        [weakSelf endRefresh];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSDictionary *dics = responseObject;
        if ([dics[@"code"] isEqualToString:@"200"]) {
//            if (weakSelf.page == 1) {
//                [weakSelf.tableArray removeAllObjects];
//                [weakSelf.tableView.mj_footer resetNoMoreData];
//            }
            [weakSelf.tableArray removeAllObjects];
            
            MineInviteModel *model = [MineInviteModel mj_objectWithKeyValues:dics[@"data"]];
            
            // 邀请码，二维码
            [weakSelf.inviteCodeLabel setText:model.referral_code];
            [weakSelf.QRCodeImageView sd_setImageWithURL:GetUrl(model.referral_url) placeholderImage:[UIImage imageNamed:@"jir"]];

            // 邀请的人列表
//            if (model.invitee_list.count == 0 && weakSelf.page != 1) {
//                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
//                weakSelf.page--;
//                return;
//            }
            
            
            for (MineInviteMemberModel *dataModel in model.invitee_list) {
                if ([dataModel isKindOfClass:[MineInviteMemberModel class]]) {
                    [weakSelf.tableArray addObject:dataModel];
                }
            }
            [weakSelf.tableView reloadData];
            
        } else {
            [MBProgressHUD showError:weakSelf.view title:@"" code:dics[@"code"] animated:YES];
            // 失败时操作
//            if (weakSelf.page != 1) {
//                weakSelf.page--;
//            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:weakSelf.view title:@"" code:@"" animated:YES];
//        [weakSelf endRefresh];
//        if (weakSelf.page != 1) {
//            weakSelf.page--;
//        }
    }];
    
}
//- (void)endRefresh {
//    [_tableView.mj_header endRefreshing];
//    [_tableView.mj_footer endRefreshing];
//}

#pragma mark - tableViewDataSource & tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.tableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MineInviteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineInviteIdentifier forIndexPath:indexPath];
    MineInviteMemberModel *model = self.tableArray[indexPath.row];
    
    // 头像
    [cell.headerImageView sd_setImageWithURL:GetUrl(model.img) placeholderImage:PlaceholderImageHeader];
    // 姓名
    [cell.nameLabel setText:model.name];
    // 时间
    [cell.timeLabel setText:[DateSomeThing getDateStrWithTimestampstr:model.createtime formatStr:@"yyyy-MM-dd HH:mm"]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44.5;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44.5)];
    [header setBackgroundColor:[UIColor whiteColor]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, header.mj_w-20, 44)];
    [label setText:@"我的邀请列表"];
    [label setTextColor:[UIColor blackTextColor]];
    [label setFont:TextPingFangFontSize(14)];
    [header addSubview:label];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, label.bottom, header.mj_w-20, 0.5)];
    [line setBackgroundColor:[UIColor borderColor]];
    [header addSubview:line];
    
    return header;
}
#pragma mark - DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
//主提示标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    return [[NSAttributedString alloc] initWithString:@"暂无邀请用户"
                                           attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0],
                                                        NSForegroundColorAttributeName:[UIColor grayTextColor]}];
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    _page = 1;
    [self getNetData];
}
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return self.tableView.mj_h-(self.tableView.mj_h - (self.tableView.tableHeaderView.mj_h+44.5))/2 - self.tableView.mj_h/2;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
